package com.cfq.scan;

import java.security.GeneralSecurityException;
import java.security.Security;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import com.sun.mail.util.MailSSLSocketFactory;

public class MailTools {
	private Properties props;
	private Session session;
	private MimeMessage mimeMsg;
	private Multipart mp;
	private static String smtp = "smtp.exmail.xxxxxxx.com";
	private static String username = "xxxxx@xxxx.com";
	private static String password = "xxxxxxxxxx";
	/**
	 * 
	 * 
	 * @param
	 * 
	 */
	public MailTools() {
		props = System.getProperties();
		props.put("mail.smtp.auth", "false");
		session = Session.getDefaultInstance(props, null);
		session.setDebug(true);
		mimeMsg = new MimeMessage(session);
		mp = new MimeMultipart();
	}

	/**
	 * 
	 * 
	 * @param smtp 邮件发送服务器
	 * @throws GeneralSecurityException
	 * 
	 */

	public MailTools(String smtp, String username, String password) throws GeneralSecurityException {
		Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
		final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
		props = System.getProperties();
		MailSSLSocketFactory sf = null;
		sf = new MailSSLSocketFactory();
		sf.setTrustAllHosts(true);
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.host", smtp);
		props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
		props.put("mail.smtp.socketFactory.fallback", "false");
		props.put("mail.smtp.ssl.enable", "true");
		props.put("mail.smtp.port", "465");
		props.put("mail.smtp.ssl.socketFactory", sf);
		props.put("mail.smtp.ssl.protocols", "TLSv1.2");
		session = Session.getInstance(props, new Authenticator() {
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(username, password);
			}
		});
		session.setDebug(true);
		mimeMsg = new MimeMessage(session);
		mp = new MimeMultipart();
	}
	/**
	 * 
	 * 发送邮件 无附件
	 * @throws MessagingException
	 */
	public boolean sendMail(String from, String[] to, String subject, String content) throws Exception {
		boolean rt = false;
		try {
			// 设置发信人
			mimeMsg.setFrom(new InternetAddress(from));
			// 设置接收人
			for (int i = 0; i < to.length; i++) {
				mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to[i]));
			}
			// 设置主题
			mimeMsg.setSubject(subject);
			// 设置正文
			BodyPart bp = new MimeBodyPart();
			bp.setContent(content, "text/html;charset=utf-8");
			mp.addBodyPart(bp);
			// 设置附件
			mimeMsg.setContent(mp);
			mimeMsg.saveChanges();
			// 发送邮件
			if (props.get("mail.smtp.auth").equals("true")) {
				Transport transport = session.getTransport("smtp");
				transport.connect((String) props.get("mail.smtp.host"), (String) props.get("username"),
						(String) props.get("password"));
				transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.TO));
				transport.close();

			} else {
				Transport.send(mimeMsg);
			}
			rt = true;
		} catch (Exception e) {
			e.printStackTrace();
			String json = to[0] + "\n";
			return rt;
		}
		return rt;
	}

	/**
	 * 简易邮件发送
	 * 
	 * @param to      收件人
	 * @param content 内容
	 */
	public void sendPersonAlertMail(String[] to, String content) {
		String subject = "自动预警--";
		try {
			MailTools email = new MailTools(smtp, username, password);
			email.sendMail(username, to, subject, content);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
